define(['jquery',
        'module/discussion/item',
        'module/home/customEvent',
        'module/home/showUpdateBox',
        'module/home/more'],
    function ($, item, customEvent, updateBox, more) {
        // we need to create update box also.



        var eventBind = false;
        var mainUpdate = function () {

            // load things for update in main arena.
            var thisAlways = this, pageDS = {maxLastSeenId: 0, minLastSeenId: 0, pageSize: 10},
                template = require('module/template/homeTemplate'), moreAdded = false;
            $('.main-content').empty();
            $('.main-content').append(template['discussion-home']);
            $('.main-content').find('.agenda-form').hide();

            // bind document with event of appending and pre pending.

            if(!eventBind){
                $(document).on(customEvent.UPDATE_DISCUSSION, function (event) {
                    thisAlways.takeUpdate(event.append);
                });
                eventBind = true;
            }


            this.takeUpdate = function (append) {

                if (append) {
                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url: '/user/getDiscussion', data: {
                            lastSeenId: pageDS.minLastSeenId,
                            limit: pageDS.pageSize,
                            after: ((pageDS.minLastSeenId == 0) ? true : false)
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            $(".main-content").children('.update-area').children('ul').append(item(value)).slideDown();
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                            pageDS.minLastSeenId = value.upid;
                        });
                        more(data.length, pageDS.pageSize, customEvent.UPDATE_DISCUSSION);
                    });

                } else {

                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url: '/user/getDiscussion', data: {
                            lastSeenId: pageDS.maxLastSeenId,
                            limit: pageDS.pageSize,
                            after: true
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            $(".main-content").children('.update-area').children('ul').prepend(item(value)).slideDown();
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                        });
                    });
                }
            };

            $.event.trigger({
                type: customEvent.UPDATE_DISCUSSION,
                append: true
            });
            // initialize update box.
           // updateBox($("#updateInput"), {updateType : '1'});

        };
        return {
            render : mainUpdate,
            create : updateBox
        };
    });